<?php

namespace app\admin\controller\finance;

use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use fast\Tree;

/**
 * 收入明细
 *
 * @icon fa fa-circle-o
 */
class Incomelog extends Backend
{

    /**
     * Incomelog模型对象
     * @var \app\admin\model\finance\Incomelog
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id,desc,amount';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\finance\Incomelog;
        $this->view->assign("statusList", $this->model->getStatusList());

        //开启模型验证
        $this->modelValidate = true;

        $groupList = collection(AuthGroup::select())->toArray();
        Tree::instance()->init($groupList);
        $groupdata = [];
        $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        foreach ($result as $k => $v) {
            $groupdata[$v['id']] = $v['name'];
        }
        $this->view->assign('groupdata', $groupdata);
    }

    /**
     * 查看
     */
    public function index()
    {

        $admin = $this->auth->getUserInfo();
        $group = $this->auth->getGroups();
        $groupId = $group[0]["group_id"];

        $this->assignconfig('groupId', $groupId);

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            if ($groupId == 1) {
                $list = $this->model
                    ->with(['admin', 'group', 'subjects', 'type', 'audit'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            } else if ($groupId == 2) {
                $list = $this->model
                    ->with(['admin', 'group', 'subjects', 'type', 'audit'])
                    ->where($where)
                    ->where(['h_id' => $admin["home_id"]])
                    ->order($sort, $order)
                    ->paginate($limit);
            } else if ($groupId == 3) {
                $list = $this->model
                    ->with(['admin', 'group', 'subjects', 'type', 'audit'])
                    ->where($where)
                    ->where(['u_id' => $admin["id"]])
                    ->order($sort, $order)
                    ->paginate($limit);
            }
  
            foreach ($list as $row) {
                $row->visible(['id', 'group_id', 'subjects_id', 'type_id', 'datatime', 'createtime', 'create_id', 'audittime', 'audit_id', 'amount', 'status', 'proofimage', 'desc', 'updatetime']);
                $row->visible(['audit']);
                $row->getRelation('audit')->visible(['nickname']);
                $row->visible(['admin']);
                $row->getRelation('admin')->visible(['nickname']);
                $row->visible(['group']);
                $row->getRelation('group')->visible(['name']);
                $row->visible(['subjects']);
                $row->getRelation('subjects')->visible(['name']);
                $row->visible(['type']);
                $row->getRelation('type')->visible(['name']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add($ids = null)
    {
        $admin = $this->auth->getUserInfo();
        $this->view->assign("admin", $admin);
        if ($ids) {
            $row = $this->model->get($ids);
            $this->view->assign("row", $row);
        }
        return parent::add();
    }
}
